package com.google.shop.product.wxlogin;

import com.alibaba.fastjson.JSON;
import com.google.shop.product.wxlogin.entity.AccessToken;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Service;

@Service
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/wxlogin/WxService.class */
public class WxService {
    private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    private static final String APPID = "wx1412818cea0faff9";
    private static final String APP_SECRET = "5cbb9d896e278ea48e1f9e83068c37e2";
    private static final String GET_ACCESS_TOKEN_URL_2 = GET_ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APP_SECRET);
    private static AccessToken accessToken = new AccessToken();

    public Map getQRCode() throws Exception {
        String ticket2 = getQRCodeTicket2();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url", "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket2);
        map.put("ticket", ticket2);
        return map;
    }

    public String getQRCodeTicket2() throws Exception {
        return ((Map) JSON.parseObject(WxUtil.sendPost("{\"expire_seconds\": 604800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}", "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + getToken()), Map.class)).get("ticket").toString();
    }

    public String getQRCodeTicket() throws Exception {
        return ((Map) JSON.parseObject(WxUtil.sendPost("{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": 123}}}", "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + getToken()), Map.class)).get("ticket").toString();
    }

    public String getToken() throws Exception {
        if (accessToken.isOut()) {
            getAccessToken();
        }
        return accessToken.getToken();
    }

    private void getAccessToken() throws Exception {
        InputStream stream = ((HttpsURLConnection) new URL(GET_ACCESS_TOKEN_URL_2).openConnection()).getInputStream();
        Map map = (Map) JSON.parse(IOUtils.toString(stream, "utf-8"));
        accessToken.setToken(map.get("access_token").toString());
        accessToken.setEndTime(Long.valueOf(new Date().getTime() + ((Long.valueOf(map.get("expires_in").toString()).longValue() - 60) * 1000)));
        stream.close();
    }

    public Map<String, String> parseXml(HttpServletRequest request) throws Exception {
        Map<String, String> map = new HashMap<>();
        InputStream inputStream = request.getInputStream();
        for (Element e : new SAXReader().read(inputStream).getRootElement().elements()) {
            map.put(e.getName(), e.getText());
        }
        inputStream.close();
        return map;
    }
}
